<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>横图管理</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
                </div>
                <div class="layui-card-body">
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="sort">
                        <input type="text" placeholder="排序" readonly autocomplete="off" class="layui-input" value="{{d.sort}}" style="height: 28px;">
                    </script>
                    <script type="text/html" id="img">
                        <img src="/uploads/{{d.img}}" alt="">
                    </script>
                    <script type="text/html" id="status">
                        {{# if (d.status == 1){ }}
                        <button class="layui-btn layui-btn-xs layui-bg-blue">显示</button>
                        {{# }else{ }}
                        <button class="layui-btn layui-btn-xs layui-bg-red">不显示</button>
                        {{# } }}
                    </script>
                    <script type="text/html" id="table-content-list">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>
                </div>
            </div>
        </div>
    </fieldset>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function(){
        var $ = layui.$,
            form = layui.form,
            table = layui.table;

        table.on('edit(demo)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            var id = obj.data.id; //所在行的所有相关id
            var field = obj.field; //当前编辑的字段名
            var value = obj.value; //得到修改后的值
            if (isNaN(value)){
                layer.msg('只能为数字', {offset: ['35%', '40%']});
                return false;
            }
            $.post("{:url('System/sort_edit')}", {id:id,field:field,value:value}, function(e){
                if (e == 1){
                    layer.msg('修改成功', {offset: ['35%', '40%']});
                    setTimeout(function(){
                        table.reload('demo');
                    }, 500);
                }else{
                    layer.msg('修改失败', {offset: ['35%', '40%']});
                }
            });
        });

        //执行渲染
        table.render({
            elem: '#demo',
            url: "{:url('System/banner')}",
            method: 'post',
            cols: [[
                    {field: 'sort', title: '排序', width: 80, templet: "#sort", edit: 'text'},
                    {field: 'img', title: '图片', templet: "#img"},
                    {field: 'link', title: '链接'},
                    {field: 'status', title: '是否显示', templet: "#status"},
                    {fixed: 'right', title: '操作', align:'center', toolbar: '#table-content-list'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;

            if(layEvent === 'edit'){ //编辑
                layer.open({
                    type: 2
                    ,title: '横幅修改'
                    ,content: '/admin/System/banner_operate/id/' + id
                    ,maxmin: true
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            } else if(layEvent === 'del'){ //删除
                layer.confirm('确定删除吗', {
                    offset: ['30%', '35%']
                },function(index){
                    //向服务端发送删除指令
                    $.ajax({
                        type: "POST",
                        url: "{:url('System/banner_del')}",
                        data: {id:id},
                        dataType: "json",
                        success: function(e){
                            if (e == 1){
                                table.reload('demo');
                                layer.msg('删除成功', {offset: ['35%', '40%']});
                            }else{
                                layer.msg('删除失败', {offset: ['35%', '40%']});
                            }
                        }
                    });
                });
            }
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加横幅'
                    ,content: "{:url('System/banner_operate')}"
                    ,maxmin: true
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
